public class State implements Comparable<State> {
	double[] weight;
	double[] sigma;
	double error;
	double numberOfWeightsToEvolve;

	State() {
		weight = new double[Data.COLS];
		sigma = new double[Data.COLS];
		error = Double.MAX_VALUE;
		numberOfWeightsToEvolve = 5;
	}
	
	void backpropegation() {
		for (int r = 0; r < Data.ROWS; r++) {
			double pred = 0;
			for (int c = 0; c < Data.COLS; c++) {
				pred += weight[c] * Data.DATA[r][c];
			}

			double real = Data.LABEL[r];
			double err = real - pred;
			double tan = Math.tanh(err);
			for (int c = 0; c < Data.COLS; c++) {
				weight[c] += 0.00000001 * tan * Data.DATA[r][c];
			}
		}
	}

	double predict(int r) {
		double pred = 0;
		for (int c = 0; c < Data.COLS; c++) {
			pred += weight[c] * Data.DATA[r][c];
		}
		return pred;
	}

	void setError() {
		double sum = 0;

		for (int r = 0; r < Data.ROWS; r++) {
			double pred = predict(r);
			double real = Data.LABEL[r];
			double err = real - pred;
			double sqe = err * err;
			sum += sqe;
		}

		error = Math.sqrt(sum / Data.ROWS);
	}

	public int compareTo(State s) {
		if (error > s.error) {
			return 1;
		} else {
			return -1;
		}
	}

	public String toString() {
		return "" + error;
	}

	public State clone() {
		State s = new State();
		System.arraycopy(weight, 0, s.weight, 0, weight.length);
		System.arraycopy(sigma, 0, s.sigma, 0, sigma.length);
		s.error = error;
		s.numberOfWeightsToEvolve = numberOfWeightsToEvolve;

		return s;
	}
}
